﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using AAA.TradeLanguage.Data;
using AAA.Base.Util;

namespace AAA.TradeLanguage
{
    public class SymbolUtil
    {
        public static int GetSettleWeekNumber(DateTime date)
        {
            int iWeek = 0;
            DateTime dtCurrentDate = new DateTime(date.Year, date.Month, 1);

            while (dtCurrentDate.CompareTo(date) <= 0)
            {
                if (dtCurrentDate.DayOfWeek == DayOfWeek.Wednesday)
                    iWeek++;
                dtCurrentDate = dtCurrentDate.AddDays(1);
            }

            return iWeek;
        }

        public static DateTime GetWeekOptionsSettleDate(DateTime dtNow)
        {
            while (dtNow.DayOfWeek != DayOfWeek.Wednesday)
                dtNow = dtNow.AddDays(1);
            return dtNow;
        }

        public static float ShiftExecutePrice(float fPrice, int iDirection)
        {
            if (fPrice <= 10000)
                return fPrice + iDirection * 100;
            else
                return fPrice + iDirection * 200;
        }

        public static float AtTheMoneyPrice(float fPrice)
        {
            float fAtMoney = (float)NumericHelper.Round(fPrice / 100.0, 0);

            if (fPrice <= 10000)
            {
                return fAtMoney * 100;
            }
            else
            {
                if (fAtMoney % 2 == 1)
                {
                    if (fPrice < fAtMoney * 100)
                        return (fAtMoney - 1) * 100;
                    else
                        return (fAtMoney + 1) * 100;
                }
                else
                {
                    return fAtMoney * 100;
                }
            }
        }

        public static ContractInfo NextMonthContract(DateTime dtCurrent)
        {
            return NextMonthContract(HotContract(dtCurrent));
        }

        public static ContractInfo NextMonthContract(ContractInfo hotContract)
        {
            ContractInfo nextMonthContract = new ContractInfo();
            nextMonthContract.Year = hotContract.Year;
            nextMonthContract.Month = hotContract.Month + 1;

            if (nextMonthContract.Month > 12)
            {
                nextMonthContract.Year += 1;
                nextMonthContract.Month = 1;
            }

            return nextMonthContract;
        }

        public static int TradeDayCountToSettleDay(DateTime dtCurrent)
        {
            int iDayCount = 0;
            DateTime dtSettleDate;
            try
            {
                dtSettleDate = SettleDate(dtCurrent.Year, dtCurrent.Month);
                dtCurrent = DateTime.Parse(dtCurrent.ToString("yyyy/MM/dd"));

                while (dtCurrent.CompareTo(dtSettleDate) <= 0)
                {
                    if (dtCurrent.DayOfWeek != DayOfWeek.Saturday &&
                       dtCurrent.DayOfWeek != DayOfWeek.Sunday)
                        iDayCount++;

                    dtCurrent = dtCurrent.AddDays(1);
                }

            }
            catch (Exception ex)
            {
                throw new Exception("SymbolUtil", ex);
            }
            return iDayCount;
        }

        public static DateTime SettleDate(int iYear, int iMonth)
        {
            DateTime dtSettleDate = new DateTime(iYear, iMonth, 1, 23, 59, 59);

            int iCount = 0;

            while (iCount < 3)
            {
                if (dtSettleDate.DayOfWeek == DayOfWeek.Wednesday)
                    iCount++;
                dtSettleDate = dtSettleDate.AddDays(1);
            }

            return DateTime.Parse(dtSettleDate.AddDays(-1).ToString("yyyy/MM/dd"));
        }

        public static ContractInfo HotContract(DateTime dtCurrent)
        {
            ContractInfo contractInfo = new ContractInfo();
            DateTime dtToday = DateTime.Parse(dtCurrent.ToString("yyyy/MM/dd"));
/*
            DateTime dtMonthStart = new DateTime(dtCurrent.Year, dtCurrent.Month, 1);
            int iCount = 0;

            while (iCount < 2)
            {
                if (dtMonthStart.DayOfWeek == DayOfWeek.Wednesday)
                    iCount++;
                dtMonthStart = dtMonthStart.AddDays(1);
            }
*/
//            if (dtCurrent <= dtMonthStart)
            if (dtToday <= SettleDate(dtCurrent.Year, dtCurrent.Month))
            {
                contractInfo.Year = dtCurrent.Year;
                contractInfo.Month = dtCurrent.Month;
            }
            else
            {
                contractInfo.Year = dtCurrent.Year;
                contractInfo.Month = dtCurrent.Month + 1;
                if (contractInfo.Month > 12)
                {
                    contractInfo.Year += 1;
                    contractInfo.Month = 1;
                }
            }

            return contractInfo;
        }

    }
}
